#!/usr/bin/env bash

# this is a simple config for herbstluftwm
# ...getting tweaked by Digit.

#  and... i'm sorting it.  mission for today (( 2022 07 09 )) :D  :::  esdf  ::  ( "  s d e f  "   as the order goes in this, following the convention i pasted from.


## dev notes
# sorely missing xmonad's stack style of control.  
# currengly cant apply shift to move tabbed windows in a shmancyherbstluftwmsplit, since i'm just mod-tabbing, and its reverse is with shift...  whereas if i were e and d 'ing..  but then it doesnt go through the stack of windows inclusive of tabs, just hops layers... :/   ... i'll suss this fun little puzzle when more fresh headed.

###  where did i put the thing that says how fast the scratchpad scrolls into view?  i want it faster yet.  fewer redraws.


hc() {
    herbstclient "$@"
}

#
hc emit_hook reload

#meh, yeah, dont really need.  may as well turn off while tweaking, ~ if only to keep the nice background i set ~ until i sort this with clever shell-fu
#xsetroot -solid '#381919'

#remove rules and start over.
#hc unrule -F

# remove all existing keybindings
hc keyunbind --all

# keybindings
# if you have a super key you will be much happier with Mod set to Mod4
#Mod=Mod1    # Use alt as the main modifier
Mod=Mod4   # Use the super key as the main modifier

#here i'll need a keybind to issue (iirc) herbstclient wmexec openbox
#hc keybind $Mod-Shift-F11 hc wmexec openbox
#hc keybind $Mod-Shift-F11 wmexec openbox
#hc keybind $Mod-Shift-F11 herbstclient wmexec openbox
# will need suss how to write it for sure.  havent tried any of these.

# oh ^   how long have i left that undone.
#hc keybind $Mod-F11 wmexec openbox 
hc keybind $Mod-Control-F11 wmexec openbox 
#^ it works  !     :D :D    both ways.   dont go silly trying press it lots though.  i'm sure that will break things.   ... stress test in safe situation only.  ;)
# and will wanna get one for xmonad too.  ;0
hc keybind $Mod-Control-F9 wmexec xmonad

#yikes, nearly pressed this... add more modifiers
hc keybind $Mod-Control-Shift-q quit
hc keybind $Mod-Shift-r reload
#hc keybind $Mod-Shift-c close #not close'y enough.
#hc keybind $Mod-Shift-c close_or_remove #closes the empty splitframe too
hc keybind $Mod-F4 close_or_remove #closes the empty splitframe too
hc keybind $Mod-Shift-F4 close_and_remove #closes the empty splitframe too
#
#hc keybind $Mod-Return spawn "${TERMINAL:-xterm}" # use your $TERMINAL with xterm as fallback
#
#hc keybind $Mod-Return spawn "${TERMINAL:-st:-xterm}" # use your $TERMINAL with xterm as fallback  ... and can haz st too?
#
#hc keybind $Mod-Return spawn "/home/digit/gittings/sts/st006/st/st-006-026" # use your $TERMINAL with xterm as fallback
#meh fuck that, might as well keep it simple
hc keybind $Mod-Return spawn "st"

# menus
#hc keybind $Mod-r spawn dmenu-bind.sh    
#hc keybind $Mod-r spawn dmenu_run
hc keybind $Mod-r spawn /home/digit/bin/dmenu-bind.sh

# basic movement in tiling and floating mode
# focusing clients
hc keybind $Mod-Left  focus left
hc keybind $Mod-Down  focus down
hc keybind $Mod-Up    focus up
hc keybind $Mod-Right focus right
hc keybind $Mod-h     focus left
hc keybind $Mod-j     focus down
hc keybind $Mod-k     focus up
hc keybind $Mod-l     focus right
hc keybind $Mod-s     focus left
hc keybind $Mod-d     focus down
hc keybind $Mod-e     focus up
hc keybind $Mod-f     focus right
#

# moving clients in tiling and floating mode
hc keybind $Mod-Shift-Left  shift left
hc keybind $Mod-Shift-Down  shift down
hc keybind $Mod-Shift-Up    shift up
hc keybind $Mod-Shift-Right shift right
hc keybind $Mod-Shift-h     shift left
hc keybind $Mod-Shift-j     shift down
hc keybind $Mod-Shift-k     shift up
hc keybind $Mod-Shift-l     shift right
hc keybind $Mod-Shift-s     shift left
hc keybind $Mod-Shift-d     shift down
hc keybind $Mod-Shift-e     shift up
hc keybind $Mod-Shift-f     shift right

# splitting frames
# create an empty frame at the specified direction
hc keybind $Mod-u       split   bottom  0.5
hc keybind $Mod-o       split   right   0.5
hc keybind $Mod-minus       split   bottom  0.5 # - for horizontal, like tmux 
hc keybind $Mod-bar       split   right   0.5 # | for vertical, like tmux
#hc keybind $Mod-h       split   bottom  0.5 # h for horizontal, like tmux # but i use for go left a frame.
#hc keybind $Mod-v       split   right   0.5 # v for vertical, like tmux # but i use for go right a workspace.

# let the current frame explode into subframes
hc keybind $Mod-Control-space split explode
# close a split ( i think... )
hc keybind $Mod-c remove

# resizing frames and floating clients
resizestep=0.02
hc keybind $Mod-Control-h       resize left +$resizestep
hc keybind $Mod-Control-j       resize down +$resizestep
hc keybind $Mod-Control-k       resize up +$resizestep
hc keybind $Mod-Control-l       resize right +$resizestep
hc keybind $Mod-Control-Left    resize left +$resizestep
hc keybind $Mod-Control-Down    resize down +$resizestep
hc keybind $Mod-Control-Up      resize up +$resizestep
hc keybind $Mod-Control-Right   resize right +$resizestep
hc keybind $Mod-Control-s       resize left +$resizestep
hc keybind $Mod-Control-d       resize down +$resizestep
hc keybind $Mod-Control-e       resize up +$resizestep
hc keybind $Mod-Control-f       resize right +$resizestep

# tags
tag_names=( {1..9} )
tag_keys=( {1..9} 0 )

hc rename default "${tag_names[0]}" || true
for i in "${!tag_names[@]}" ; do
    hc add "${tag_names[$i]}"
    key="${tag_keys[$i]}"
    if ! [ -z "$key" ] ; then
        hc keybind "$Mod-$key" use_index "$i"
        hc keybind "$Mod-Shift-$key" move_index "$i"
    fi
done

# cycle through tags
hc keybind $Mod-period use_index +1 --skip-visible
hc keybind $Mod-comma  use_index -1 --skip-visible
hc keybind $Mod-v use_index +1 --skip-visible
hc keybind $Mod-x use_index -1 --skip-visible
#hc keybind $Mod-"]" use_index +1 --skip-visible   ... idk if that's how to do that... 
#hc keybind $Mod-"[" use_index -1 --skip-visible   ... idk if that's how to do that... 
#
# and will want
# cycle through tags and take current window with you  (... maybe?  or?)
# and a -Shift- version which moves the current window there ?
hc keybind $Mod-Shift-v move_index +1 --skip-visible
hc keybind $Mod-Shift-x move_index -1 --skip-visible

# layouting
#hc keybind $Mod-r remove #needs rebound, mod-r's for dmenu.
#hc keybind $Mod-s floating toggle 
hc keybind $Mod-t floating toggle #tiling toggle.  ;D  smart.
#hc keybind $Mod-f fullscreen toggle
hc keybind $Mod-g fullscreen toggle
#hc keybind $Mod-Shift-f set_attr clients.focus.floating toggle
#hc keybind $Mod-b spawn /home/digit/gittings/herbstluftwm/scripts/maximise.sh  #toggle #maximise.sh ?  ize! XD
hc keybind $Mod-b spawn maximize.sh
hc keybind $Mod-Shift-g set_attr clients.focus.floating toggle #idk is this a pop-out-to-float bind?
#hc keybind $Mod-Shift-m set_attr clients.focus.minimized true
hc keybind $Mod-Shift-c set_attr clients.focus.minimized true #if this is minimise...
#hc keybind $Mod-Control-m jumpto last-minimized
hc keybind $Mod-Control-c jumpto last-minimized #if this is unminimise... ... i should try add that to openbox too.
#hc keybind $Mod-p pseudotile toggle
hc keybind $Mod-Shift-t pseudotile toggle ###... hrmm... o_O  compare against floating toggle on mod-t... this is just inside the frame... i think.
# The following cycles through the available layouts within a frame, but skips
# layouts, if the layout change wouldn't affect the actual window positions.
# I.e. if there are two windows within a frame, the grid layout is skipped.
hc keybind $Mod-space                                                           \
            or , and . compare tags.focus.curframe_wcount = 2                   \
                     . cycle_layout +1 vertical horizontal max vertical grid    \
               , cycle_layout +1

# mouse
hc mouseunbind --all
hc mousebind $Mod-Button1 move
hc mousebind $Mod-Button2 zoom
hc mousebind $Mod-Button3 resize

# focus
hc keybind $Mod-BackSpace   cycle_monitor
hc keybind $Mod-Tab         cycle_all +1
hc keybind $Mod-Shift-Tab   cycle_all -1
#hc keybind $Mod-c cycle  #( using for remove, for now.)
hc keybind $Mod-Shift-space cycle
hc keybind $Mod-i jumpto urgent

#digbinds scratchpad of kiybends ;D

hc keybind $Mod-Shift-F6 spawn xkill
hc keybind $Mod-z spawn scrot /home/digit/images/scrots/
#        <command>scrot /home/digit/images/scrots/</command>
#digbinds notion,
# have mod shift e/d be jumping splits.
# have mod e/d be traversing the stack (like mod tab and mod shift tab)

# theme
hc attr theme.tiling.reset 1
hc attr theme.floating.reset 1
hc set frame_border_active_color '#ffffff'
hc set frame_border_normal_color '#573838'
hc set frame_bg_normal_color '#575757'
#hc set frame_bg_active_color '#345F0Caa' #default
hc set frame_bg_active_color '#ff8800' #digits
hc set frame_border_width 1
hc set always_show_frame on
hc set frame_bg_transparent on
hc set frame_transparent_width 1
hc set frame_gap 0

hc attr theme.title_height 7
hc attr theme.title_font 'nztt:pixelsize=6'  # example using Xft
# hc attr theme.title_font '-*-fixed-medium-r-*-*-13-*-*-*-*-*-*-*'
hc attr theme.padding_top 1  # space below the title's baseline (i.e. text depth)
hc attr theme.active.color  '#60492F' #digits      default:'#345F0Cef'
hc attr theme.title_color '#ffffff'
hc attr theme.normal.color '#323232'
hc attr theme.urgent.color '#7811A1'
hc attr theme.normal.title_color '#898989'
hc attr theme.inner_width 1
hc attr theme.inner_color black
hc attr theme.border_width 1
hc attr theme.floating.border_width 1
hc attr theme.floating.outer_width 1
hc attr theme.floating.outer_color black
hc attr theme.active.inner_color '#789161'
hc attr theme.urgent.inner_color '#9A65B0'
hc attr theme.normal.inner_color '#606060'
# copy inner color to outer_color
for state in active urgent normal ; do
    hc substitute C theme.${state}.inner_color \
        attr theme.${state}.outer_color C
done
hc attr theme.active.outer_width 1
hc attr theme.background_color '#222222'

#trying to get active border white...
#i made this up, blind, didnt look up what.
#hc attr theme.border_active_color '#000000'
#hc attr theme.border_active_color '#ffffff'

hc set window_gap 0
#hc set frame_padding 3
hc set frame_padding 0
hc set smart_window_surroundings off
hc set smart_frame_surroundings on
#hc set mouse_recenter_gap 1 1 #seems null effect
hc set mouse_recenter_gap 1

# rules
hc unrule -F
#hc rule class=XTerm tag=3 # move all xterms to tag 3
hc rule focus=on # normally focus new clients
hc rule floatplacement=smart
#hc rule focus=off # normally do not focus new clients
# give focus to most common terminals
#hc rule class~'(.*[Rr]xvt.*|.*[Tt]erm|Konsole)' focus=on
hc rule windowtype~'_NET_WM_WINDOW_TYPE_(DIALOG|UTILITY|SPLASH)' floating=on
hc rule windowtype='_NET_WM_WINDOW_TYPE_DIALOG' focus=on
hc rule windowtype~'_NET_WM_WINDOW_TYPE_(NOTIFICATION|DOCK|DESKTOP)' manage=off

hc set tree_style '╾│ ├└╼─┐'

# unlock, just to be sure
hc unlock

# do multi monitor setup here, e.g.:
# hc set_monitors 1280x1024+0+0 1280x1024+1280+0
# or simply:
# hc detect_monitors

# find the panel
panel=~/.config/herbstluftwm/panel.sh
[ -x "$panel" ] || panel=/etc/xdg/herbstluftwm/panel.sh
for monitor in $(hc list_monitors | cut -d: -f1) ; do
    # start it on each monitor
    "$panel" "$monitor" &
done

#####
####
###
##
#

# Digit's herbstluftwm wishlist

#     
##    ( ideas/snippets/jank/pseudocode goes here. )
###
####
##### 

# bring WINID
# gonna want some of that.  :)

# mirror   <- is in the manual, called that.
#  horizontal|vertical|both   ... both?  curious... not like a toggle h/v in xmonad.

# multi-window scratchpads
#  herb3terminal.sh can do this, said bv in #herbstluftwm on libera.chat
# q3terminal.sh is wat they meant  so, i copied it to ~/bin and chmodded, now just bind it to what your want your main dynamic scratchpad to be bound to...  ... when i remember gow to bind keys in herbstluftwm.  h3rbinal.sh

#hc keybind $Mod-Shift-F6 spawn xkil
#hc keybind $Mod-F3 spawn q3terminal.sh #main dynamic scratchpad

#hrm, gotta rebind this.  for some reason doesnt work on kinesis advantage's soft Function buttons.
hc keybind $Mod-F3 spawn h3rbinal.sh #main dynamic scratchpad
# for some reason, kinesis advantage's f3 is XF86AudioPrev, not F3, in xev.
hc keybind $Mod-XF86AudioPrev spawn h3rbinal.sh #main dynamic scratchpad
#better one
hc keybind $Mod-w spawn h3rbinal.sh #main dynamic scratchpad

#... simple as that?
#yup, tho mibi better on
#hc keybind $Mod-` spawn h3rbinal.sh #main dynamic scratchpad
#hc keybind $Mod-'`' spawn h3rbinal.sh #main dynamic scratchpad
#hc keybind $Mod-"`" spawn h3rbinal.sh #main dynamic scratchpad
#hc keybind $Mod-\` spawn h3rbinal.sh #main dynamic scratchpad
#hc keybind $Mod-"\`" spawn h3rbinal.sh #main dynamic scratchpad
#hc keybind $Mod-'\`' spawn h3rbinal.sh #main dynamic scratchpad
#hc keybind $Mod-"`" spawn h3rbinal.sh #main dynamic scratchpad
#hc keybind $Mod-\` spawn h3rbinal.sh #main dynamic scratchpad
# well, this works
hc keybind $Mod-Escape spawn h3rbinal.sh #main dynamic scratchpad
# thnx rudi_s. ` = grave. "how X handles" & man:keysymdef.h:"XK_grave" 
hc keybind $Mod-grave spawn h3rbinal.sh #main dynamic scratchpad
